package com.azxcrm.project.system.customer.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.azxcrm.common.utils.security.ShiroUtils;
import com.azxcrm.framework.web.controller.BaseController;
import com.azxcrm.framework.web.domain.Message;
import com.azxcrm.framework.web.page.TableDataInfo;
import com.azxcrm.project.system.basic.domain.CustomerModifyRecord;
import com.azxcrm.project.system.basic.service.ICustomerModifyRecordService;
import com.azxcrm.project.system.customer.domain.CustomerAttendance;
import com.azxcrm.project.system.customer.domain.CustomerInfo;
import com.azxcrm.project.system.customer.domain.CustomerIntegralRecord;
import com.azxcrm.project.system.customer.service.ICustomerAttendanceService;
import com.azxcrm.project.system.customer.service.ICustomerInfoService;
import com.azxcrm.project.system.customer.service.ICustomerIntegralRecordService;
import com.azxcrm.project.system.dict.domain.DictData;
import com.azxcrm.project.system.dict.service.IDictDataService;
import com.azxcrm.project.system.order.domain.OrderDeal;
import com.azxcrm.project.system.order.service.IOrderDealService;

/**
 * 客户 控制层处理
 * 
 * @author Dustyone
 * @date 2018-09-04
 */
@Controller
@RequestMapping("/module/customerInfo")
public class CustomerInfoController extends BaseController 
{
	/**
     * 客户工作类型
     */
	@Value("${crm.dictionary.customer.job}")
	private String job;
	
	/**
     * 客户背景类型
     */
    @Value("${crm.dictionary.customer.background}")
    private String customerBackground;
    
    /**
     * 客户学历类型
     */
    @Value("${crm.dictionary.customer.education}")
    private String education;
	
    private String prefix = "module/customerInfo";
    
    @Autowired
    private ICustomerModifyRecordService customerModifyRecordService;
    
	@Autowired
	private ICustomerInfoService customerInfoService;
	
	@Autowired
	private ICustomerAttendanceService customerAttendanceService;
	
	@Autowired
	private IDictDataService dictDataService;
	
	@Autowired
	private IOrderDealService orderDealService;
	
	@Autowired
	private ICustomerIntegralRecordService customerIntegralRecordService;

	@GetMapping()
	@RequiresPermissions("module:customerInfo:view")
	String customerInfo() {
		return prefix + "/customerInfo";
	}

	/**
	 * 查询客户列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("module:customerInfo:list")
	@ResponseBody
	public TableDataInfo list(CustomerInfo customerInfo) {
		setPageInfo(customerInfo);
		List<CustomerInfo> list = customerInfoService.selectCustomerInfoList(customerInfo);
		return getDataTable(list);
	}

	/**
	 * 客户线索详情页面
	 */
	@GetMapping("/details/{customerId}")
	public String details(@PathVariable("customerId") Integer customerId,OrderDeal orderDeal, Model model)
	{
		//根据ID查询客户信息
		CustomerInfo customerInfo = customerInfoService.selectCustomerInfoById(customerId);
		model.addAttribute("customerInfo",customerInfo);		
		
		
		//查询成单退单信息
		orderDeal.setCustomerIdentity(customerInfo.getIdentityNum());
		List<OrderDeal> orderDeals = orderDealService.selectOrderDealList(orderDeal);
		model.addAttribute("orderDeals",orderDeals);
		
		//查询客户客户参课信息
		List<CustomerAttendance> customerAttendance = customerAttendanceService.selectCustomerAttendanceByCellphone(customerInfo.getCellphone());
		model.addAttribute("customerAttendanceList",customerAttendance);
		
		//查询客户操作记录
		List<CustomerModifyRecord> customerModifyRecord = customerModifyRecordService.selectCustomerModifyRecordByCustomerId(customerId);
		model.addAttribute("customerModifyRecordList", customerModifyRecord);
		
		//查询积分流水信息
		CustomerIntegralRecord customerIntegralRecord = new CustomerIntegralRecord();
		customerIntegralRecord.setCustomerId(customerId);
		List<CustomerIntegralRecord> customerIntegralRecordList = customerIntegralRecordService.selectCustomerIntegralRecordList(customerIntegralRecord);
		model.addAttribute("customerIntegralRecordList", customerIntegralRecordList);
		
		String username=ShiroUtils.getLoginName();
		model.addAttribute("username", username);
	    return prefix + "/details";
	}
	
	/**
	 * 新增客户
	 */
	@GetMapping("/add")
	@RequiresPermissions("module:customerInfo:add")
	public String add(Model model)
	{		
		List<DictData> list = dictDataService.selectDictDataByDictType(customerBackground);
		model.addAttribute("list", list);	
		
		List<DictData> educationList = dictDataService.selectDictDataByDictType(education);
		model.addAttribute("educationList", educationList);
		
		List<DictData> jobList = dictDataService.selectDictDataByDictType(job);
		model.addAttribute("jobList", jobList);
		return prefix + "/add";
	}

	/**
	 * 修改客户
	 */
	@GetMapping("/edit/{customerId}")
	@RequiresPermissions("module:customerInfo:edit")
	public String edit(DictData dictData, @PathVariable("customerId") Integer customerId,Model model) 
	{
		CustomerInfo customerInfo = customerInfoService.selectCustomerInfoById(customerId);
		model.addAttribute("customerInfo", customerInfo);
		dictData.setDictType(customerBackground);

		List<DictData> dictList = dictDataService.selectDictDataList(dictData);	
		model.addAttribute("dictList",dictList);
		
		List<DictData> jobList = dictDataService.selectDictDataByDictType(job);
		model.addAttribute("jobList", jobList);
		
		List<DictData> educationList = dictDataService.selectDictDataByDictType(education);
		model.addAttribute("educationList", educationList);
		
		return prefix + "/edit";
	}

	/**
	 * 修改客户积分
	 */
	@GetMapping("/editIntegral/{customerId}")
	@RequiresPermissions("module:customerInfo:edit")
	public String editIntegral(@PathVariable("customerId") Integer customerId,Model model) 
	{
		CustomerInfo customerInfo = customerInfoService.selectCustomerInfoById(customerId);
		model.addAttribute("customerInfo", customerInfo);
		CustomerIntegralRecord customerIntegralRecord = customerIntegralRecordService.selectCustomerIntegralRecordByCustomerIdNewRecord(customerId);
		model.addAttribute("customerIntegralRecord", customerIntegralRecord);

		return prefix + "/editIntegral";
	}
	
	
	/**
	 * 新增客户积分流水
	 */
	@ResponseBody
	@PostMapping("/saveCustomerIntegralRecord")
	@RequiresPermissions("module:customerInfo:add")
	public Message saveCustomerIntegralRecord(CustomerIntegralRecord customerIntegralRecord) {
		customerIntegralRecord.setSource(0);
		if (customerIntegralRecordService.saveCustomerIntegralRecord(customerIntegralRecord) > 0) {
			CustomerInfo customerInfo = customerInfoService.selectCustomerInfoById(customerIntegralRecord.getCustomerId());
			customerInfo.setIntegral(customerIntegralRecord.getIntegralTotal());
			customerInfoService.updateCustomerInfo(customerInfo);
			return Message.ok();
		}	
		return Message.error("积分不足");
	}
	
	/**
	 * 保存客户
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("module:customerInfo:add")
	public Message save(CustomerInfo customerInfo) {
		if (customerInfoService.saveCustomerInfo(customerInfo) > 0) {
			return Message.ok();
		}		
		return Message.error();
	}

	/**
	 * 
	 * 删除客户
	 */
	@PostMapping("/remove/{customerId}")
	@ResponseBody
	@RequiresPermissions("module:customerInfo:remove")
	public Message remove(@PathVariable("customerId") Integer customerId) {
		
		CustomerInfo customerInfo = customerInfoService.selectCustomerInfoById(customerId);
		//强制判断身份证号码
		if(customerInfo.getIdentityNum() ==null || "".equals(customerInfo.getIdentityNum()) ){
			return Message.error(1, "该客户信息异常，请联系管理员");
		}
		
		if(orderDealService.checkCustomerInfoCasecaded(customerInfo.getIdentityNum())){
			//该客户信息已被引用，不予以删除
			return Message.error(1, "该客户信息已被引用，不予以删除");
		}
		
		if (customerInfoService.deleteCustomerInfoById(customerId) > 0) {
			return Message.ok();
		}
		return Message.error();
	}

	/**
	 * 批量删除客户
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("module:customerInfo:batchRemove")
	public Message remove(@RequestParam("ids[]") Integer[] customerIds) {
		
		for(int i = 0;i<customerIds.length;i++){
			CustomerInfo customerInfo = customerInfoService.selectCustomerInfoById(customerIds[i]);
			//强制判断身份证号码
			if(customerInfo.getIdentityNum() ==null || "".equals(customerInfo.getIdentityNum()) ){
				return Message.error(1, "客户信息  ["+customerInfo.getName()+"]异常，请联系管理员");
			}
			
			if(orderDealService.checkCustomerInfoCasecaded(customerInfo.getIdentityNum())){
				//该客户信息已被引用，不予以删除
				return Message.error(1, "客户  ["+customerInfo.getName()+"]已被引用，不予以删除");
			}
		}
		
		int rows = customerInfoService.batchDeleteCustomerInfo(customerIds);
		if (rows > 0) {
			return Message.ok();
		}
		return Message.error();
	}

	/**
	 * 校验客户手机唯一性
	 */
	@PostMapping("/checkCellphone")
	@ResponseBody
	public String checkCellphoneNum(CustomerInfo customerInfo) {
		String uniqueFlag = "0";
		if (customerInfo != null) {
			uniqueFlag = customerInfoService.checkCellphone(customerInfo.getCellphone());
		}
		return uniqueFlag;
	}

	/**
	 * 修改客户手机校验唯一性
	 */
	@PostMapping("/updateCellphone")
	@ResponseBody
	public String updateCellphoneNum(CustomerInfo customerInfo) {
		CustomerInfo info = customerInfoService.selectCustomerInfoById(customerInfo.getCustomerId());
		if (customerInfo.getCellphone().equals(info.getCellphone())) {
			return "0";
		}
		return checkCellphoneNum(customerInfo);

	}
}
